package com.mantru.iotpot.Adapter.Base

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Build
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import android.util.Log
import android.util.SparseArray
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import com.mantru.iotpot.R
import com.squareup.picasso.Picasso

open class AppHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    private val mViews: SparseArray<View?>

    init {
        mViews = SparseArray()
    }

    fun <V : View?> getView(@IdRes res: Int): V? {
        var view = mViews[res]
        if (view == null) {
            view = itemView.findViewById(res)
            mViews.put(res, view)
        }
        return view as V?
    }

    fun setText(
        @IdRes res: Int,
        text: CharSequence?
    ): AppHolder {
        val view = getView<View>(res)!!
        if (view is TextView) {
            view.text = text
        }
        return this
    }
    fun setTextColor(
        @IdRes res: Int,
        color: Int
    ): AppHolder {
        
        val view = getView<View>(res)!!
        
        if (view is TextView) {
            
            view.setTextColor(color)
            
        }
        
        return this
    }

    fun setBackgroundColor(
        @IdRes res: Int,
        color: Int
    ): AppHolder {
        
        val view = getView<View>(res)!!
        
        if (view is TextView) {
            
            view.setBackgroundColor(color)
            
        }
        
        return this
    }

    fun setBackground(
        @IdRes res: Int,
        @DrawableRes bg: Int
    ): AppHolder {
        
        val view = getView<View>(res)
        
        view?.setBackgroundResource(bg)
        
        return this
    }

    fun setBackground(
        @IdRes res: Int,
        bg: String?
    ): AppHolder {
        val view = getView<ImageView>(res)
        if (view != null) {
            view.setImageDrawable(null)
            Picasso.get().cancelRequest(view)

            view.setBackgroundResource(R.drawable.photo1)

            if ( bg!!.isNotEmpty() ){
                Picasso.get().load(bg).into(view)
            }
        }
        return this
    }

    fun setText(
        @IdRes id: Int,
        @SuppressLint("SupportAnnotationUsage") @StringRes text: String?
    ): AppHolder {
        val view = getView<View>(id)!!
        if (view is TextView) {
            view.text = text
        }
        return this
    }

    fun setText(
        @IdRes id: Int,
        @SuppressLint("SupportAnnotationUsage") @StringRes text: SpannableStringBuilder?
    ): AppHolder {
        val view = getView<View>(id)!!
        if (view is TextView) {
            view.text = text
        }
        return this
    }

    fun setUnderLineText(
        @IdRes id: Int,
        @SuppressLint("SupportAnnotationUsage") @StringRes text: String
    ): AppHolder {
        
        val view = getView<View>(id)!!
        
        if (view is TextView) {
            
            val spannableString = SpannableString(text)
            
            val underlineSpan = UnderlineSpan()
            
            val foregroundColorSpan = ForegroundColorSpan(Color.parseColor("#4260FF"))
            
            view.highlightColor = Color.parseColor("#00000000")
            
            spannableString.setSpan(underlineSpan, 0, text.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
            
            spannableString.setSpan(
                foregroundColorSpan,
                0,
                text.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            
            view.movementMethod = LinkMovementMethod.getInstance()
            
            view.text = spannableString
            
        }
        
        return this
    }

    fun setText(
        @IdRes id: Int,
        @StringRes text: Int
    ): AppHolder {
        
        val view = getView<View>(id)!!
        
        if (view is TextView) {
            
            view.setText(text)
            
        }
        
        return this
    }

    //    public AppHolder setImage(@IdRes int id, @DrawableRes int drawable){
    //        View view = getView(id);
    //        if (view instanceof ImageView) {
    //            ((ImageView) view).setImageResource(drawable);
    //        }
    //        return this;
    //    }
    //
    //    public AppHolder setImageBitmap(@IdRes int id, Bitmap bitmap){
    //        if (bitmap == null) {
    //            return this;
    //        }
    //        View view = getView(id);
    //        if (view instanceof ImageView) {
    //            ((ImageView) view).setImageBitmap(bitmap);
    //        }
    //        return this;
    //    }
    fun setVisibility(
        @IdRes id: Int,
        visible: Boolean
    ): AppHolder {
        
        val view = getView<View>(id)
        
        if (view != null) {
            
            val vis: Int
            
            vis = if (visible) View.VISIBLE else View.GONE
            
            setvisi(view, vis)
            
        }
        
        return this
    }

    fun setFocusable(
        @IdRes id: Int,
    ): AppHolder {
        
        val view = getView<View>(id)

        
        if (view != null) {
            
            view.isFocusable = true
            
        }
        
        return this
    }

    fun setFocusableInTouchMode(
        @IdRes id: Int,
    ): AppHolder {
        
        val view = getView<View>(id)
        
        if (view != null) {
            
            view.setFocusableInTouchMode(true)
            
        }
        
        return this
    }

    fun setProgress(@IdRes id: Int, progress: Int): AppHolder {
        val progressBar = getView<ProgressBar>(id)
        progressBar?.progress = progress
        return this
    }

    fun requestFocus(
    @IdRes id: Int,
    ): AppHolder {
        
        val view = getView<View>(id)
        
        if (view != null) {
            
            view.requestFocus()
            
        }
        
        return this
    }

    fun getVisibility(
        @IdRes id: Int
    ): Boolean {
        
        val view = getView<View>(id)
        
        if (view != null) {
            
            return view.visibility == View.VISIBLE
        }
        
        return false
    }

    private fun setvisi(v: View, vis: Int) {
        
        v.visibility = vis
        
    }

    //
    //    public AppHolder setRating(@IdRes int id, float rate){
    //        View view = getView(id);
    //        if (view instanceof RatingBar) {
    //            ((RatingBar) view).setRating(rate);
    //
    //        }
    //        return this;
    //    }
    fun setOnClickListener(
        @IdRes id: Int,
        listener: View.OnClickListener?
    ): AppHolder {
        
        val view = getView<View>(id)!!
        
        view.setOnClickListener(listener)
        
        return this
    }
}
